package com.woniu.defecttracking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.defecttracking.entity.Account;
import com.woniu.defecttracking.entity.AccountRole;
import com.woniu.defecttracking.entity.Role;
import com.woniu.defecttracking.exception.AccountException;
import com.woniu.defecttracking.exception.RoleException;
import com.woniu.defecttracking.mapper.AccountMapper;
import com.woniu.defecttracking.mapper.AccountRoleMapper;
import com.woniu.defecttracking.mapper.RoleMapper;
import com.woniu.defecttracking.param.UpdateAccountRoleParam;
import com.woniu.defecttracking.service.AccountRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.defecttracking.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-31
 */
@Service
public class AccountRoleServiceImpl extends ServiceImpl<AccountRoleMapper, AccountRole> implements AccountRoleService {

    @Autowired
    private RoleService roleService;

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private RoleMapper roleMapper;

    /**
    * @作者: 倪西
    * @描述：查询账号对应的角色，拼接角色名
    * @时间：2022/1/7 22:15
    * @参数：[id]
    * @返回：java.lang.String
    **/
    @Override
    public String getRoles(Integer id) {
        QueryWrapper<AccountRole> accountRoleQueryWrapper = new QueryWrapper<>();
        accountRoleQueryWrapper.eq("account_id", id);
        List<AccountRole> list = list(accountRoleQueryWrapper);
        List<Integer> roles = list.stream().map(a -> a.getRoleId()).collect(Collectors.toList());
        if (roles==null||roles.size()<=0){
            return null;
        }
        List<Role> roles1 = roleService.listByIds(roles);
        List<String> collect = roles1.stream().map(a -> a.getRoleName()).collect(Collectors.toList());
        String name = "";
        for (String s : collect) {
            name+=s+",";
        }
        if (name.length()>=2)
            name=name.substring(0,name.length()-1);
        return name;
    }

    /**
    * @作者: 倪西
    * @描述：获取账号对应的角色id
    * @时间：2022/1/7 22:17
    * @参数：[id]
    * @返回：java.util.List<java.lang.Integer>
    **/
    @Override
    public List<Integer> getRoleIds(Integer id) {
        List<AccountRole> accountRoles = list(new QueryWrapper<AccountRole>().eq("account_id", id));
        List<Integer> roles = accountRoles.stream().map(a -> a.getRoleId()).collect(Collectors.toList());
        return roles;
    }

    /**
    * @作者: 倪西
    * @描述：修改账号对应的授权角色关联
    * @时间：2022/1/7 22:18
    * @参数：[updateAccountRoleParam]
    * @返回：java.lang.Boolean
    **/
    @Override
    public Boolean saveAccountRoles(UpdateAccountRoleParam updateAccountRoleParam) throws AccountException, RoleException {
        Account account = accountMapper.selectById(updateAccountRoleParam.getAccountId());
        if (null==account){
            throw new AccountException("账户不存在",2001);
        }

        remove(new QueryWrapper<AccountRole>().eq("account_id",updateAccountRoleParam.getAccountId()));

        if (updateAccountRoleParam.getRoleIds()==null||updateAccountRoleParam.getRoleIds().size()<=0){
            return true;
        }
        List<Role> roles = roleMapper.selectBatchIds(updateAccountRoleParam.getRoleIds());
        if (roles.size()!=updateAccountRoleParam.getRoleIds().size()){
            throw new RoleException("有不存在的角色",2051);
        }

        List<AccountRole> accountRoles = new ArrayList<>();
        for (Integer id:updateAccountRoleParam.getRoleIds()){
            AccountRole accountRole = new AccountRole();
            accountRole.setAccountId(updateAccountRoleParam.getAccountId());
            accountRole.setRoleId(id);
            accountRoles.add(accountRole);
        }
        return saveBatch(accountRoles);
    }
}
